package tech.aistar.controller;


import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import tech.aistar.mapper.UserInfoMapper;
import tech.aistar.pojo.User;
import tech.aistar.pojo.UserInfo;
import tech.aistar.pojo.result.ResultReturn;

import tech.aistar.pojo.vo.UserInfoVo;
import tech.aistar.service.UserInfoService;
import tech.aistar.utils.JwtUtils;


import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/userInfo")
@Slf4j
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RedisTemplate redisTemplate;






    @GetMapping("/{email}")
    public ResultReturn<String> sendCode(@PathVariable("email") String email){
        //1. 先判断用户是否存在
        User user = userInfoService.selectByEmail(email);
        if(user == null)
            return ResultReturn.fail("sorry,email is not exist!");
        //2. 说明email是存在的,则发送验证码
        userInfoService.sendCode(email);
        return ResultReturn.success("验证码已经发送!请查看邮箱!");
    }



    @PostMapping("/{email}/{code}/{password}")
    public ResultReturn updataPassword(@PathVariable("email") String email,@PathVariable("code") String code,@PathVariable("password") String password){
        //1. 先判断用户是否存在
        User user = userInfoService.selectByEmail(email);
        if(user == null)
            return ResultReturn.fail("sorry,email is not exist!");
        //2. 说明email是存在的,校验验证码
        //根据key到redis中获取验证码
        String serverCode = (String) redisTemplate.opsForValue().get(email);
        //3. 判断验证码是否正确
        if(serverCode == null){
            return ResultReturn.fail("验证码已过期!");
        }

//        4. 验证码是存在的,则登录成功
        if(serverCode.equals(code)){
            userInfoService.updatePwdByEmail(email,password);
            String token = JwtUtils.generateToken(user);
            return ResultReturn.success("修改成功");
        };
//        5. 未知情况
        return ResultReturn.success("<UNK>!");
    }

    @GetMapping("/info")
    public ResultReturn<UserInfoVo> selectById(HttpServletRequest request){
        Integer userId = (Integer)request.getAttribute("userId");
        UserInfoVo userInfoVo = userInfoService.selectInfoByUser_id(userId);
        System.out.println(userId);
        System.out.println("++++++++"+userInfoVo);
        return ResultReturn.success(userInfoVo);

    }
    @GetMapping("{name}/{gender}/{phone}/{address}/{introduction}")
    public ResultReturn<String> updateUser(HttpServletRequest request,
                                           @PathVariable("name") String name,
                                           @PathVariable("gender") String gender,
                                           @PathVariable("phone") String phone,
                                           @PathVariable("address") String address,
                                           @PathVariable("introduction") String introduction){
        Integer userId = (Integer)request.getAttribute("userId");
        System.out.println("++++++++"+userId);
        User user = new User();
        user.setUserId(userId);
        user.setUsername(name);
        user.setPhone(phone);
        int i = userInfoService.updateUserByUser_id(userId,name,gender,phone,address,introduction);
        UserInfo info = new UserInfo();
        info.setSex(gender);
        info.setAddress(address);
        if(i > 0){
            return ResultReturn.success("修改成功");
        }
        return ResultReturn.fail("修改失败");
    }
}
